import axios from "axios";
import { getToken } from './auth'
import {Toast} from "vant";

// const baseUrl = process.env.BASE_URL
const baseUrl = 'http://127.0.0.1:4523/m1/3292286-0-default/'
const service = axios.create({
    baseURL: baseUrl,
    method: 'post',
    timeout: 5000
})

service.interceptors.request.use(
    config => {
        const token = getToken()
        if(token) {
            config.headers['Authorization'] = token
        }
        config.headers['Content-Type'] = 'application/json'
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        const data = response.data
        const code = data.code || '999999'
        console.info(response)
        if (code !== '000000') {
            Toast(data.msg || '系统异常：' + data.code)
            return Promise.reject('请求接口异常')
        }
        return response.data
    },
    error => {
        return Promise.reject(error)
    }
)

export default service